<template>
  <main
    class="site-content"
    :class="{
      'site-content--tabs': $route.meta.isTab,
      'site-content_home': topMenu == 'tm01',
    }"
  >
    <!-- 主入口标签页 s -->
    <el-tabs
      v-if="$route.meta.isTab"
      v-model="mainTabsActiveName"
      :closable="$route.meta.isTabClose"
      @tab-click="selectedTabHandle"
      @tab-remove="removeTabHandle"
       :class="{'el-tabs-card':$store.state.user.publisherType == 'tjjt'}"
    >
      <el-dropdown class="site-tabs__tools" :show-timeout="0">
        <i class="el-icon-arrow-down el-icon--right"></i>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item
            @click.native="tabsCloseCurrentHandle"
            v-if="$route.meta.isTabClose"
            >关闭当前标签页</el-dropdown-item
          >
          <el-dropdown-item @click.native="tabsCloseOtherHandle"
            >关闭其它标签页</el-dropdown-item
          >
          <el-dropdown-item
            @click.native="tabsCloseAllHandle"
            v-if="$route.meta.isTabClose"
            >关闭全部标签页</el-dropdown-item
          >
          <el-dropdown-item @click.native="tabsRefreshCurrentHandle"
            >刷新当前标签页</el-dropdown-item
          >
        </el-dropdown-menu>
      </el-dropdown>
      <el-tab-pane
        v-for="item in mainTabs"
        :key="item.name"
        :label="item.title"
        :name="item.name"
      >
        <el-card :body-style="siteContentViewHeight">
          <iframe
            v-if="item.type === 'iframe'"
            :src="item.iframeUrl"
            width="100%"
            height="100%"
            frameborder="0"
            scrolling="yes"
          >
          </iframe>
          <!--<keep-alive v-else>
            <router-view v-if="item.name === mainTabsActiveName" />
          </keep-alive>-->
          <section class="app-main" v-else>
            <transition name="fade-transform" mode="out-in">
              <keep-alive>
                <router-view v-if="item.name === mainTabsActiveName" />
              </keep-alive>
            </transition>
          </section>
        </el-card>
      </el-tab-pane>
    </el-tabs>
    <!-- 主入口标签页 e -->
    <el-card v-else :body-style="siteContentViewHeight">
      <keep-alive>
        <router-view />
      </keep-alive>
    </el-card>
  </main>
</template>

<script>
import { isURL } from "@/utils/validate";
export default {
  data() {
    return {};
  },
  computed: {
    documentClientHeight: {
      get() {
        return this.$store.state.common.documentClientHeight;
      },
    },
    menuActiveName: {
      get() {
        return this.$store.state.common.menuActiveName;
      },
      set(val) {
        this.$store.commit("common/updateMenuActiveName", val);
      },
    },
    mainTabs: {
      get() {
        return this.$store.state.common.mainTabs;
      },
      set(val) {
        this.$store.commit("common/updateMainTabs", val);
      },
    },
    mainTabsActiveName: {
      get() {
        return this.$store.state.common.mainTabsActiveName;
      },
      set(val) {
        this.$store.commit("common/updateMainTabsActiveName", val);
      },
    },

    pathParm: {
      get() {
        return this.$store.state.common.pathParm;
      },
      set(val) {
        this.$store.commit("common/updatePathParm", val);
      },
    },
    siteContentViewHeight() {
      var height = this.documentClientHeight - 50 - 30 - 2;
      if (this.$route.meta.isTab) {
        height -= 40;
        return isURL(this.$route.meta.iframeUrl)
          ? { height: height + "px" }
          : { minHeight: height + "px" };
      }
      return { minHeight: height + "px" };
    },
    topMenu: {
      get() {
        return this.$store.state.common.topMenu;
      },
      set(val) {
        this.$store.commit("common/updateTopMenu", val);
      },
    },
  },
  methods: {
    // tabs, 选中tab
    selectedTabHandle(tab) {
      console.log("this.mainTabs:",this.mainTabs)
      tab = this.mainTabs.filter((item) => item.name === tab.name);
      console.log("tab:",tab)
      if (tab.length >= 1) {
        //切换tab不丢失参数
        var tabName = tab[0].name;
        if (tabName.includes("commonMod-iframe-common")) {
          // 执行替换操作
          tabName = tabName.replace("common-", "common.");
        }
        var parm = this.pathParm;
        if (parm[tabName]) {
          tabName = tabName + "?" + parm[tabName];
        }
        console.log("tabName:",tabName)
        if (tab[0].id||tab[0].nolassFlag) {
          //zwq 2023-12-29点击tab 修改
          this.$router.push({
            path: tab[0].tabName,
            query: {
              ...tab[0],
              name:'',
              tit: tab[0].title.substr(tab[0].title.indexOf('-')+1) ,//tab[0].title.split("-")[1],
              tabs: true
            },
          });

           //原来版本
          /*this.$router.push({
            path: tab[0].tabName,
            query: {
              id: tab[0].id,
              tit:tab[0].tit,
              nolassFlag:tab[0].nolassFlag,
              tabs: true,
            },
          });*/
        } else if ( tab[0].addFlag) {
           
          this.$router.push({
            path: tab[0].tabName,
            query: {
              tabs: true,
              jumpPara: tab[0].addFlag,
              addFlag: tab[0].addFlag 
            },
          }); 
        } else {
          this.$router.push({ path: tabName });
        }
      }
    },
    // tabs, 删除tab
    removeTabHandle(tabName) {
      var parm = this.pathParm;
      parm[tabName] = "";
      this.pathParm = parm;

      let obj = {}
      let keyName = ''
      for (const key in this.$store.state.common.tableSel) {
        if (key.includes(tabName)) {
          keyName = key
        }
      }
      if (keyName) {
        obj[keyName] = 0
        this.$store.commit('common/setTableSel',obj)
      } 
      this.mainTabs = this.mainTabs.filter((item) => item.name !== tabName);
      console.log(this.mainTabs)
      if (this.mainTabs.length >= 1) {
        var tab = this.mainTabs.filter((item) => item.name !== tabName);
        
        if (tab.length >= 1) { 
          var tab = this.mainTabs.filter((item) => item.name !== tabName);
          //切换tab不丢失参数
          // var tabName0 = tab[0].name;
          console.log("删除tab：",tab,tab.slice(-1))
          var tabName0 = tab.slice(-1)[0].name;
          if (tabName0.includes("commonMod-iframe-common")) {
            // 执行替换操作
            tabName0 = tabName0.replace("common-", "common.");
          }
          if (tab.slice(-1)[0].id||tab.slice(-1)[0].nolassFlag ||tab.slice(-1)[0].addFlag) {
            if ( tab.slice(-1)[0].addFlag) { 
                this.$router.push({
                  path: tab.slice(-1)[0].tabName,
                  query: {
                    tabs: true,
                    jumpPara: tab.slice(-1)[0].addFlag,
                    addFlag: tab.slice(-1)[0].addFlag 
                  },
                }); 
            }else{
              console.log("删除tab2：",tab,tab.slice(-1))
              //zwq 2023-12-29点击tab 修改
              this.$router.push({
                path: tab.slice(-1)[0].tabName,
                query: {
                  ...tab.slice(-1)[0],
                  name:'',
                  tit:tab.slice(-1)[0].title.substr(tab.slice(-1)[0].title.indexOf('-')+1) ,//tab.slice(-1)[0].tit.split("-")[1],
                  tabs: true,
                },
              });
            }
            
            //原来版本
            /*this.$router.push({
              path: tab.slice(-1)[0].tabName,
              query: {
                id: tab.slice(-1)[0].id,
                nolassFlag: tab.slice(-1)[0].nolassFlag,
                tabs: true,
              },
            });*/

          }
          else {
            var parm = this.pathParm;

            if (parm[tabName0]) {
              tabName0 = tabName0 + "?" + parm[tabName0];
            }
            this.$router.push({ path: tabName0 });
          }
          // this.mainTabsActiveName = this.$route.name
        }
        // 当前选中tab被删除
        /*  if (tabName === this.mainTabsActiveName) {
            this.$router.push({ name: this.mainTabs[this.mainTabs.length - 1].name }, () => {
              this.mainTabsActiveName = this.$route.name
            })
          }*/
      } else {
        this.menuActiveName = "";
        this.$router.push({ name: "home" });
        this.$store.commit('common/setTableSel','')
      }
    },
    // tabs, 关闭当前
    tabsCloseCurrentHandle() {
      this.removeTabHandle(this.mainTabsActiveName);
      var parm = this.pathParm;
      parm[this.mainTabsActiveName] = "";
      this.pathParm = parm;
    },
    // tabs, 关闭其它
    tabsCloseOtherHandle() {
      this.mainTabs = this.mainTabs.filter(
        (item) => item.name === this.mainTabsActiveName
      );

      var parm = this.pathParm;
      var notParm = parm[this.mainTabsActiveName];
      this.pathParm = {};
      this.pathParm[this.mainTabsActiveName] = notParm;
    },
    // tabs, 关闭全部
    tabsCloseAllHandle() {
      this.mainTabs = [];
      this.menuActiveName = "";
      this.pathParm = {};
      this.$router.push({ name: "home" });
    },
    // tabs, 刷新当前
    tabsRefreshCurrentHandle() {
      var tempTabName = this.mainTabsActiveName;
      this.removeTabHandle(tempTabName);
      this.$nextTick(() => {
        if (tempTabName == "home"){
          let currentTime = new Date();
          let formattedTime = currentTime.toISOString();
          this.$router.push({
            name: tempTabName,
            query: { currentTime: formattedTime }  // 将格式化后的时间点作为参数传递
          });
        }else {
          this.$router.push({ name: tempTabName });
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
/*框架起作用的*/
.app-main {
  height: calc(100vh - 120px);
  width: 100%;
  position: relative;
  overflow: auto;
  overflow-x: hidden;
  padding: 10px;
  padding-bottom: 0;
  margin-bottom: 0px;
  //box-sizing: border-box;
  background: #fff;
  background-size: 600px 450px;
}

.fixed-header + .app-main {
  // padding-top: 50px;
  padding: 70px 20px 20px;
}

.hasTagsView {
  .app-main {
    height: calc(100vh - 100px);
  }

  .fixed-header + .app-main {
    padding: 104px 20px 20px;
  }
}
.el-tabs-card /deep/ .el-tabs__header{
  .el-tabs__item{ 
  .el-icon-close{
    position: relative;
    width: 16px;
    height: 16px;
    &::before{
      position: absolute;
      background-size: 16px 16px;
      width: 16px;
      height: 16px;
      left: 50%;
      top: 3px;
      transform: translateX(-50% );
    }
  }
}
}
</style>
<style>
.site-content > .el-tabs > .el-tabs__content > .site-tabs__tools {
  background: #f2f4f8 !important;
  color: #909399;
}

.site-content_home > .el-tabs > .el-tabs__header {
  left: 0px;
  padding-left: 20px;
}
.site-sidebar--fold .site-content_home > .el-tabs > .el-tabs__header {
  left: 0px;
}
</style>
